package org.xiaodongwei.jvm.op;

import org.xiaodongwei.jvm.exe.JVMStack;
import tech.medivh.classpy.classfile.bytecode.Bipush;
import tech.medivh.classpy.classfile.bytecode.Instruction;
import tech.medivh.classpy.classfile.constant.ConstantPool;
import tech.medivh.classpy.classfile.jvm.Opcode;

public class BiPushOpCodeExecute extends OpCodeExecute{

    private JVMStack jvmStack;

    public BiPushOpCodeExecute(JVMStack jvmStack, ConstantPool constantPool) {
        this.opCode = Opcode.bipush;
        this.jvmStack = jvmStack;
        this.constantPool = constantPool;
    }

    // TODO: 目前只能解析出int类型，其他类型会出现问题，目前的虚拟机不支持, 后续扩展
    @Override
    public void execute(Instruction instruction) {
        Bipush bipush = (Bipush) instruction;
        String desc = bipush.getDesc();
        jvmStack.peek().getOperateStack().push(parsePushValue(desc));
    }

    private Object parsePushValue(String desc) {
        if (desc == null || desc.isEmpty()) return null;
        String valString = desc.split(" ")[1];
        Integer integer = Integer.parseInt(valString);
        return integer;
    }
}
